package com.act.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

/**
 * @description HTTP工具类
 * @author Javis
 */
public class HttpUtil {

    private static final Logger LOGGER = Logger.getLogger(HttpUtil.class);
    private static final int CONNECTION_TIMEOUT = 50000;
    private static final int SOCKET_TIMEOUT = 50000;
    private static final int CONNECTION_REQUEST_TIMEOUT = 50000;
    private static RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(SOCKET_TIMEOUT)
            .setConnectTimeout(CONNECTION_TIMEOUT)
            .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
            .build();

    /**
     * GET
     * @param url
     * @return
     */
    public static String doGet(String url) {
        System.out.println("GET请求链接：" + url);
        HttpClient client = HttpClients.createDefault();
        HttpEntity httpEntity = null;
        try {
            HttpGet httpGet = new HttpGet(url);
            httpGet.setConfig(requestConfig);
            HttpResponse response = client.execute(httpGet);
            httpEntity = response.getEntity();
            return EntityUtils.toString(httpEntity);

        } catch (Exception e) {
            System.out.println(e);
    
        }
        return null;
    }

    /**
     * POST
     * @param url
     * @param content
     * @return
     */
    public static String doPost(String url, String content) {
        System.out.println("POST请求正文：" + url);
        HttpClient client = HttpClients.createDefault();
        HttpEntity httpEntity = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            httpPost.setConfig(requestConfig);
            if (content != null) {
                httpPost.setEntity(new StringEntity(content, "UTF-8"));
            }
            HttpResponse response = client.execute(httpPost);
            httpEntity = response.getEntity();
            String result = EntityUtils.toString(httpEntity);
            return result;

        } catch (Exception e) {

        }
        return null;
    }

    /**
     * POST
     * @param url
     * @return
     */
    public static String doDelete(String url) {
        System.out.println("DELETE请求正文：" + url);
        HttpClient client = HttpClients.createDefault();
        HttpEntity httpEntity = null;
        try {
            HttpDelete httpDelete = new HttpDelete(url);
            httpDelete.setConfig(requestConfig);
            HttpResponse response = client.execute(httpDelete);
            httpEntity = response.getEntity();
            String result = EntityUtils.toString(httpEntity);
            return result;

        } catch (Exception e) {
           
        }
        return null;
    }

    /**
     * POST
     * @param url
     * @param content
     * @return
     */
    public static String doPatch(String url, String content) {
        System.out.println("PATCH请求正文：" + url);
        HttpClient client = HttpClients.createDefault();
        HttpEntity httpEntity = null;
        try {
            HttpPatch httpPatch = new HttpPatch(url);
            httpPatch.setHeader("Content-Type", "application/x-www-form-urlencoded");
            httpPatch.setHeader("x-http-method-override", "PATCH");
            httpPatch.setConfig(requestConfig);
            if (content != null) {
                httpPatch.setEntity(new StringEntity(content, "UTF-8"));
            }
            HttpResponse response = client.execute(httpPatch);
            httpEntity = response.getEntity();
            String result = EntityUtils.toString(httpEntity);
            return result;

        } catch (Exception e) {
      
        }
        return null;
    }
}
